import express from 'express'
export const router = express.Router()
import store from '../../store/admin.js'
import { createConnectionPool } from '../../mysql/index.js'

const connection = await createConnectionPool()
//获取商户用户列表
router.get('/adminApi/GetAdminAccount', async (req, res) => {
  const DataArr = await store.getAdminData()
  //判断是否等于数组
  console.log(DataArr)
  if (DataArr instanceof Array)
    res.status(200).send({ code: 1, message: '获取数据成功', data: DataArr })
  else res.status(400).send({ code: 400, message: `获取数据失败` })
})

//添加商户用户列表
// router.post('/adminApi/AddAdminAccount', async (req, res) => {
//   const MerchantInfo = await store.getAdminData()
//   // console.log(`查看用户输入内容`, req.body, '终止查看')
//   // let sql = `
//   //   INSERT INTO user (username, nickname, password, email, points, fee_rate, status, mistake_count, create_time,last_login_time, deleted)   
//   //   SELECT ?,?,?,?,?,?,?,?,?,?,?,?  
//   //   WHERE NOT EXISTS (SELECT 1 FROM user WHERE username = ? AND password = ?);
//   // `
//   // const params = [req.body.userName, req.body.userName, req.body.password, '', 0, 0, 0, '', '', new Date(), new Date(), 0, req.body.userName, req.body.password]
//   // await connection.execute(sql, params)
//   const isMerchant = MerchantInfo.find(
//     user =>
//       user.userName === req.body.userName &&
//       user.password === req.body.password &&
//       user.Acode === req.body.Acode &&
//       user.is == req.body.is
//   )
//   if (req.body.iF == 0) {
//     delete req.body.iF
//     const str = await store.addAdminData(req.body)
//     if (str == '添加成功') {
//       return res.status(200).send({ code: 1, message: '6666' })
//     } else {
//       return res.status(400).send({ code: 0, message: '提交失败' })
//     }
//   }
//   if (!isMerchant)
//     return res.status(200).send({ code: 0, message: '账号密码错误' })
//   else return res.status(200).send({ code: 1, message: '登录成功' })
// })

//删除商户用户列表
router.delete('/adminApi/DeleteAdminAccount', async (req, res) => {
  console.log(`查看我的`, req.body.id)
  let str = await store.delAdminData(req.body.id)
  if (str == '删除成功') {
    res.status(200).send({ code: 1, message: '删除成功' })
  } else {
    res.status(400).send({ code: 400, message: '删除失败' })
  }
})

export default router
